﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using UIT.Converters;

namespace UIT.Controls.Data
{
    [Flags]
    public enum Operations
    {
        None = 0,
        Select = 1,
        Insert = 2,
        Delete = 4,
        Update = 8,
        // +1 because if we can sort, we definitely can select
        Sort = 16,
        // +1 because if we can paginate, we definitely can select
        Page = 32
    }

    public class SelectArgument
    {
        public int StartRowIndex { get; private set; }
        public int MaximumRows { get; private set; }
        public virtual int TotalRowCount { get; set; }

        public string SortExpression { get; private set; }

        public SelectArgument Page(int startRowIndex, int maximumRows)
        {
            StartRowIndex = startRowIndex;
            MaximumRows = maximumRows;
            RequestedOperations = RequestedOperations | Operations.Page;
            return this;
        }

        public SelectArgument Sort(string column, ListSortDirection direction = ListSortDirection.Ascending)
        {
            switch (direction)
            {
                case ListSortDirection.Ascending:
                    SortExpression = column;
                    break;
                case ListSortDirection.Descending:
                    SortExpression = column + " DESC";
                    break;
            }
            RequestedOperations = RequestedOperations | Operations.Sort;
            return this;
        }

        public Operations RequestedOperations { get; private set; }
    }

    [TypeConverter(typeof(DataProviderConverter))]
    public interface IDataProvider : IDisposable, IContainable<IControl>
    {
        string Id { get; set; }

        System.Collections.IEnumerable Select(SelectArgument arguments);
        void Update();
        void Insert();
        void Delete();

        Operations SupportedOperations { get; }
    }

    public interface IParametersDataProvider : IDataProvider
    {
        ParameterCollection SelectParameters { get; }
        ParameterCollection UpdateParameters { get; }
        ParameterCollection InsertParameters { get; }
        ParameterCollection DeleteParameters { get; }
    }
}
